/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.pg.misra.rules;

import java.util.ArrayList;
import java.util.List;
import pl.pg.misra.AST.AST;
import pl.pg.misra.Interfaces.IRule;

/**
 *
 * @author Paweł
 */
public class ReturnRequiredRule extends AbstractRule implements IRule{

    @Override
    public List<String> checkRule(List<AST> asts) {
        List<String> result = new ArrayList<>();
        
        for (AST functionNode : asts){
            if (!functionNode.getAttributes().get(0).contains("void")){
                List<AST> searchResult = new ArrayList<>();
                
                searchResult = findNodesByType("ReturnStmt", functionNode, searchResult);
                
                if (searchResult.isEmpty()){
                    result.add(buildViolationMessage(functionNode, "16.8", "function which returns type must have return statement"));
                }
                else{
                    for (AST returnNode : searchResult){
                        if (returnNode.getChildrens().isEmpty()){
                            result.add(buildViolationMessage(functionNode, "16.8", "return statement has to have return expression"));
                        }
                    }
                }
            }
        }
        
        return result;
    }
    
}
